import type { ProductLevel } from '@/typings/api';
import type { IEpisode } from '@/typings/interface';
import { queryVideoState } from '@/api/video';

export function convertLevel(level?: ProductLevel) {
  switch (level) {
    case 'JUNIOR':
      return '初级';
    case 'MIDDLE':
      return '中级';
    case 'SENIOR':
      return '高级';
    default:
      return '未知';
  }
}

// 判断是否购买过此课程或者这一集是否免费
export async function isFreeEpisode(episode: Pick<IEpisode, 'free' | 'product_id'>) {
  const { base } = useToast();
  const { isLogin } = $(useUser());
  if (!isLogin) {
    base('请先登录');
    return false;
  }

  if (!episode.free) return true;

  // 判断是否购买过此课程
  const { code } = await queryVideoState(episode.product_id);

  if (code === 0) return true;
  base('请先购买');
  return false;
}
